<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Giaobans extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->load->helper("url");
		$this->load->database();
	}
	public function getTypeGiaoban($id_type_giaoban=1){
		$this->db->where('id_type_giaoban',$id_type_giaoban);
		$query = $this->db->get('vtv4_type_giaoban');
		return $query->row();
	}
	public function addTypeGiaoban($data){
		return $this->db->insert('vtv4_type_giaoban',$data);
	}
	public function editTypeGiaoban($data,$id_type_giaoban){
		$this->db->where('id_type_giaoban',$id_type_giaoban);
		return $this->db->update('vtv4_type_giaoban',$data);
	}
	public function delTypeGiaoban($id_type_giaoban){
		$this->db->where('id_type_giaoban',$id_type_giaoban);
		return $this->db->delete('vtv4_type_giaoban');
	}
	public function updateTrangchu($id_type_giaoban){
		if($this->db->update('vtv4_type_giaoban',array('trangchu'=>0))){
			$this->db->where('id_type_giaoban',$id_type_giaoban);
			return $this->db->update('vtv4_type_giaoban',array('trangchu'=>1));
		}
		else
			return FALSE;
	}
	public function tieude_giaoban($id_giaoban){
		$this->db->where('id_giaoban',$id_giaoban);
		$query = $this->db->get('vtv4_giaoban');
		return $query->row()->tieude_giaoban;
	}
	public function top()
	{
		$this->db->where('type_giaoban',9);
		$query=$this->db->get('vtv4_giaoban');
		return $query->row();
	}
	public function listGiaoban(){
		$this->db->order_by('cha_giaoban ASC');
		$this->db->order_by('order_giaoban DESC');
		$query = $this->db->get('vtv4_type_giaoban');
		return $query->result();
	}
	public function multiGiaoban($listMenu,$menu_cha=0,$id_active=null){
		$str = array();
		$stylelist = array(
          'src' => 'images/Icon_01.png',          
          'class' => 'stylelist',          
          'rel' => 'lightbox',
		);
		$pre_ext = "<a href='";
		$url = "giaoban/viewall/";
		$la_ext = "</a>";
		$class_no_active = "'>".img($stylelist);
		$class_active = "' class='active_side_menu menu_list_first'>".img($stylelist);
		
		if(isset($listMenu)){			
			foreach($listMenu[$menu_cha] as $con){
				if($this->active($id_active,$con->id_type_giaoban))				
					$str[$pre_ext.site_url($url.$con->id_type_giaoban).$class_active.$con->menu_name_giaoban.$la_ext] = $this->multiGiaoban($listMenu,$con->id_type_giaoban,$id_active);
				else 
					$str[$pre_ext.site_url($url.$con->id_type_giaoban).$class_no_active.$con->menu_name_giaoban.$la_ext] = $this->multiGiaoban($listMenu,$con->id_type_giaoban,$id_active);				
			}
		}
		return $str;
	}
	//id_check : id menu tin duoc chon
	//id_hientai: id menu tin duoc duyet den
	public function active($id_check, $id_hientai){	
		$active = false;	
		if($id_check == $id_hientai) $active = true;
		else{			
			//print $this->getCha_giaoban($id_check);
			$this->db->where('id_type_giaoban',$id_check);			
			$query = $this->db->get('vtv4_type_giaoban');
			if($query->row()->cha_giaoban == $id_hientai) $active = true;
			else if($this->getCha_giaoban($id_check) == $id_hientai) $active = true;
		}
		return $active;		
	}
	private function getCha_giaoban($id_type_giaoban){
		$cha = $id_type_giaoban;
		$this->db->where('id_type_giaoban',$id_type_giaoban);			
		$query = $this->db->get('vtv4_type_giaoban');		
		if($query->row()->cha_giaoban != 0)
			$cha = $this->getCha_giaoban($query->row()->cha_giaoban);	
		return $cha;
	}
	
	public function trangchu(){
		$this->db->distinct();
		$this->db->where('trangchu',1);
		$this->db->order_by('order_giaoban DESC');
		$query = $this->db->get('vtv4_type_giaoban');
		return $query->row()->id_type_giaoban;
	}
	public function getAllGiaoban($type)
	{
		$this->db->order_by('ngay_giaoban','DESC');
		$this->db->where('type_giaoban',$type);
		$query = $this->db->get('vtv4_giaoban');
		return $query->result();
	}
	public function getGiaoban($id_giaoban)
	{
		$this->db->select('A.*,B.tenthat_nguoidung');
		$this->db->from('vtv4_giaoban A');
		$this->db->join('vtv4_nguoidung B', 'A.id_nguoidung = B.id_nguoidung');
		$this->db->where('id_giaoban',$id_giaoban);
		$query = $this->db->get();
		return $query->row();
	}
	public function getPage($date=null,$type,$id)
	{
		if($date == null)
			$date = date("Y-m-d");
		//cha
		$this->db->select('A.*,B.tenthat_nguoidung,C.menu_name_giaoban');
		$this->db->from('vtv4_giaoban A');
		$this->db->join('vtv4_nguoidung B', 'A.id_nguoidung = B.id_nguoidung');		
		$this->db->join('vtv4_type_giaoban C', 'A.type_giaoban = C.id_type_giaoban');
		$this->db->where('type_giaoban',$type);
		$this->db->get();
		$quer_cha = $this->db->last_query();
		//con cap 1
		$this->db->select('A.*,B.tenthat_nguoidung,C.menu_name_giaoban');
		$this->db->from('vtv4_giaoban A');
		$this->db->join('vtv4_nguoidung B', 'A.id_nguoidung = B.id_nguoidung');
		$this->db->join('vtv4_type_giaoban C', 'A.type_giaoban = C.id_type_giaoban');
		$this->db->where('C.cha_giaoban',$type);
		$this->db->get();
		$quer_con = $this->db->last_query();
		
		$query = $quer_cha." UNION ".$quer_con." ORDER BY ngay_giaoban DESC LIMIT ".$id.",15";
		$query = $this->db->query($query);
		//if($type==3)
		//$this->db->like('ngay_giaoban',$date);
		//$this->db->order_by('ngay_giaoban','DESC');
		//$this->db->limit(15,$id);
		return $query->result();
	}
	public function getSoluongGiaoban($type)
	{
		$this->db->order_by('ngay_giaoban','DESC');
		$this->db->where('type_giaoban',$type);
		$query = $this->db->get('vtv4_giaoban');
		return $query->num_rows();
	}
	public function insertGiaoban($data)
	{
		return $this->db->insert('vtv4_giaoban',$data);
	}
	public function updateGiaoban($data,$id_giaoban)
	{
		$this->db->where('id_giaoban',$id_giaoban);
		return $this->db->update('vtv4_giaoban',$data);
	}
	public function delGiaoban($id_giaoban)
	{
		$this->db->where('id_giaoban',$id_giaoban);
		return $this->db->delete('vtv4_giaoban');
	}
}